/**
 * Created by Administrator on 15-8-3.
 */
var ReproResource = RichBase.extend({
	statics : {
		resource : {},
		cache_resource : {},
		api : "../../module/pft_resource/api.php",
		tpl : {
			provOption : '<li class="selOption prov" data-id="<%=provid%>"><%=prov%></li>',
			cityOption : '<li class="selOption city" data-id="<%=cityid%>" data-provid="<%=provid%>"><%=city%></li>',
			resourceItem : '<li class="resouceItem" data-id="<%=id%>" data-cityid="" data-provid=""><%=title%></li>'
		}
	},
	EVENTS : {
		"click" : {
			".pSelectUl .selOption" : "onPSelectOptionClick",
			".cSelectUl .selOption" : "onCSelectOptionClick",
			".sourceUl .resouceItem" : "onResourceItemClick",
			".sureBtn" : "onsureBtnClick",
//			".newBtn" : "onnewBtnClick",
			".cancelBtn" : "onCancelBtnClick"
		},
		"input propertychange" : {
			".filterBox .filterInp" : "onFilterInpChange"
		}
	},
	init : function(opt){
		var that = this;
		var container = this.container = opt.container;
		this.pSelect = container.find(".pSelectUl");
		this.cSelect = container.find(".cSelectUl");
		this.sourceUl = container.find(".sourceUl");
		this.initProvSelect(this.pSelect);
		this.on("cSelectOptionClick",function(data){
			var cityid = data.cityid;
			var provid = data.provid;
			that.queryResource(provid,cityid);
		})
	},
	initProvSelect : function(container){
		var that = this;
		var provs = PFT.Config.Provinces;
		var tpl = this.statics.tpl.provOption;
		var html = "";
		for(var i in provs){
			var prov = provs[i];
			var provid = prov["area_id"];
			var provname = prov["area_name"];
			html += that.parseTemplate(tpl,{ provid:provid, prov:provname});
		}
		container.html(html);
		setTimeout(function(){
			container.children().first().trigger("click");
			that.fire("provsInit");
		},10)
	},
	queryResource : function(provid,cityid){
		var that = this;
		var api = this.statics.api;
		var source = this.statics.resource;
		var d = source[provid+"_"+cityid];
		if(d) return this.buildSource(d);
		PFT.Ajax({
			url : api,
			type : "get",
			dataType : "json",
			data : {
				action : "QUERY_RESOURCE",
				city : cityid,
				prov : provid
			},
			loading : function(){},
			removeLoading : function(){
				that.sourceUl.attr("data-cityid",cityid).attr("data-provid",provid);
			},
			timeout : function(){},
			serverError : function(){}
		},function(res){
			var code = res.code;
			if(code==200){
				that.buildSource(res.collection);
				source[provid+"_"+cityid] = res.collection;
			}else if(code==204){ //空数据
				that.buildSource([]);
				source[provid+"_"+cityid] = [];
			}else{

			}
		})
	},
	getCityByProv : (function(){
		var citys = {};
		return function(provid){
			var that = this;
			var cSelect = this.cSelect;
			if(!provid) return false;
			if(citys[provid]){
				cSelect.html(citys[provid]);
				setTimeout(function(){
					var active = cSelect.children(".active");
					active = active.length ? active : cSelect.children().first();
					active.trigger("click");
				},10)
				return false;
			}
			PFT.Ajax({
				url : "../../call/getAreas.php?act=getCity&area_deeppath=2",
				type : "get",
				dataType : "json",
				data : {
					parentId : provid
				}
			},function(res){
				var html = "";
				var tpl = that.statics.tpl.cityOption;
				for(var i in res){
					var city = res[i];
					var cityid = city["area_id"];
					var cityname = city["area_name"];
					html += that.parseTemplate(tpl,{
						cityid : cityid,
						city : cityname,
						provid : provid
					})
				}
				citys[provid] = html;
				cSelect.html(html);
				setTimeout(function(){
					var active = cSelect.children(".active");
					active = active.length ? active : cSelect.children().first();
					active.trigger("click");
				},10)
			})
		}
	})(),
	onPSelectOptionClick : function(that,e){
		var tarOpt = $(e.currentTarget);
		var provid = tarOpt.attr("data-id");
		if(tarOpt.hasClass("active")) return false;
		tarOpt.addClass("active").siblings().removeClass("active");
		that.pSelect.attr("data-select",provid);
		that.getCityByProv(provid);
		$("#resproFilterInp").attr("data-provid",provid);
	},
	onCSelectOptionClick : function(that,e){
		var tarOpt = $(e.currentTarget);
		var cityid = tarOpt.attr("data-id");
		var provid = tarOpt.attr("data-provid");
		if(tarOpt.hasClass("active")) return false;
		tarOpt.addClass("active").siblings().removeClass("active");
		that.cSelect.attr("data-select",cityid);
		$("#resproFilterInp").attr("data-cityid",cityid);
		that.fire("cSelectOptionClick",{cityid:cityid,provid:provid});
	},
	onResourceItemClick : function(that,e){
		var tarItem = $(e.currentTarget);
		var id = tarItem.attr("data-id");
		var name = tarItem.text();
		that.sourceUl.attr("data-select",tarItem.attr("data-id"));

	},
	onFilterInpChange : function(that,e){
		var tarInp = $(e.currentTarget);
		var val = tarInp.val();
//		var cityid = that.sourceUl.attr("data-cityid");
//		var provid = that.sourceUl.attr("data-provid");
		var cityid = tarInp.attr("data-cityid");
		var provid = tarInp.attr("data-provid");
		var source = that.statics.resource[provid+"_"+cityid];
		var result = [];
		if(!source) return false;
		for(var i in source){
			var json = {};
			var res = source[i];
			var title = res["title"];
			if(!val || title.indexOf(val)>=0){
				for(var a in res){
					json[a] = res[a];
				}
				result.push(json);
			}
		}
		that.buildSource(result)
	},
	onResourceItemClick : function(that,e){
		$(e.currentTarget).addClass("active").siblings().removeClass("active");
	},
	onsureBtnClick : function(that,e){
		var select = that.getSelect();
		var sourceid = select.sourceid;
		if(!sourceid) return false;
		that.fire("resource.select",{id:sourceid});
		that.close();
	},
	onCancelBtnClick : function(that,e){
		that.close();
	},
	onnewBtnClick : function(that,e){
		window.location.href = "pft_resource.html?m=add";
	},
	buildSource : function(data){
		var that = this;
		var html = "";
		var tpl = this.statics.tpl.resourceItem;
		for(var i in data){
			var re = data[i];
			var id = re["id"];
			var title = re["title"];
			html += that.parseTemplate(tpl,{
				id : id,
				title : title
			})
		}
		if(!html){
			html += '<li style="width:100%; height:200px; line-height:200px; text-align:center; border-bottom:0 none">没有资源</li>';
		}
		this.sourceUl.html(html);
	},
	getSelect : function(){
		return{
			provid : this.pSelect.attr("data-select"),
			cityid : this.cSelect.attr("data-select"),
			sourceid : this.sourceUl.children(".active").attr("data-id") || ""
		}
	},
	show : function(){
		$("#bg").show();
		$("#reproContainer").show();
	},
	close : function(){
		$("#bg").hide();
		$("#reproContainer").hide();
	}
})

